import 'package:flutter_module/res/colors.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

/// 点击可加载更多的列表
class LoadableListView extends StatefulWidget {
  /// 控制器
  final RefreshController controller;

  /// 列表 注意必须是sliver
  final Widget child;

  /// 头部
  final Widget? header;

  final double headerExtent;

  final bool headerPinned;

  /// 加载更多回调
  final VoidCallback? loadCall;

  /// 没有更多数据文案
  final String noDataText;

  /// 加载更多文案
  final String loadText;

  const LoadableListView(
      {Key? key,
      this.loadCall,
      required this.controller,
      required this.child,
      this.header,
      this.noDataText = '没有更多数据了~',
      this.loadText = '查看更多',
      this.headerExtent = 100,
      this.headerPinned = true})
      : super(key: key);

  @override
  State<LoadableListView> createState() => _LoadableListViewState();
}

class _LoadableListViewState extends State<LoadableListView> {
  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        //如果有头部添加头部
        if (widget.header != null)
          // SliverToBoxAdapter(
          //   child: widget.header!,
          // ),
          SliverPersistentHeader(
            delegate: _SimplePersistentHeaderDelegate(
                widget.headerExtent, widget.header!),
            pinned: widget.headerPinned,
          ),
        //列表
        widget.child,
        //加载更多脚部
        SliverToBoxAdapter(
          child: Obx(() {
            return _buildFootMore();
          }),
        )
      ],
    );
  }

  /// 加载更多
  Widget _buildFootMore() {
    if (widget.controller.isLoading) {
      return Container(
        height: 46,
        alignment: Alignment.center,
        child: const SizedBox(
          width: 20,
          height: 20,
          child: CircularProgressIndicator(
            strokeWidth: 3,
            color: Colors.redAccent,
          ),
        ),
      );
    }

    return GestureDetector(
      onTap: () {
        if (widget.controller.isIdle) {
          widget.controller.loadMore();
          if (widget.loadCall != null) {
            widget.loadCall!();
          }
        }
      },
      child: Container(
        height: 46,
        alignment: Alignment.center,
        child: Text(
          widget.controller.isNoData ? widget.noDataText : widget.loadText,
          style: const TextStyle(fontSize: 14, color: YouthColors.color_969699),
        ),
      ),
    );
  }
}

/// 刷新 加载控制器
class RefreshController {
  Rx<LoadStatus>? status;

  RefreshController(this.status);

  bool get isIdle => status?.value == LoadStatus.idle;

  bool get isLoading => status?.value == LoadStatus.loading;

  bool get isNoData => status?.value == LoadStatus.noData;

  /// 加载完成
  void loadComplete() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      status?.value = LoadStatus.idle;
    });
  }

  /// 没有更多数据
  void loadNoData() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      status?.value = LoadStatus.noData;
    });
  }

  /// 加载更多
  void loadMore() {
    // WidgetsBinding.instance.addPostFrameCallback((_) {
    status?.value = LoadStatus.loading;
    // });
  }

  void dispose() {
    status = null;
  }
}

/// 加载的状态
enum LoadStatus {
  /// 加载中
  loading,

  /// 没有数据
  noData,

  ///空闲
  idle
}

class _SimplePersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final double headerExtent;
  final Widget header;

  _SimplePersistentHeaderDelegate(this.headerExtent, this.header);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return header;
  }

  @override
  double get maxExtent => headerExtent;

  @override
  double get minExtent => headerExtent;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
